<?php

namespace App\Admin\Controllers;

use App\Models\FitnessForm;
use App\Models\User;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Validator;

class FitnessFormController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new FitnessForm(), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('title');
            $grid->column('user_id');
            $grid->column('height');
            $grid->column('weight');
            $grid->column('arm_cir_left');
            $grid->column('arm_cir_right');
            $grid->column('side_head_zlw');
            $grid->column('side_head_qy');
            $grid->column('side_head_hy');
            $grid->column('side_cvb_zxz');
            $grid->column('side_cvb_qq');
            $grid->column('side_cvb_qz');
            $grid->column('side_pat_zlw');
            $grid->column('side_pat_nx');
            $grid->column('side_thv_zxz');
            $grid->column('side_thv_ht');
            $grid->column('side_thv_qz');
            $grid->column('side_thv_fgb');
            $grid->column('side_luv_zxz');
            $grid->column('side_luv_qq');
            $grid->column('side_luv_qz');
            $grid->column('side_ntp_zlw');
            $grid->column('side_ntp_qq');
            $grid->column('side_ntp_hq');
            $grid->column('side_sar_zlw');
            $grid->column('side_sar_cs');
            $grid->column('back_nec_zlw');
            $grid->column('back_nec_qq');
            $grid->column('back_nec_xz');
            $grid->column('back_sho_zlw');
            $grid->column('back_sho_yg');
            $grid->column('back_sho_zg');
            $grid->column('back_pat_zlw');
            $grid->column('back_pat_qq');
            $grid->column('back_pat_hsk');
            $grid->column('back_tlv_zlw');
            $grid->column('back_tlv_s');
            $grid->column('back_tlv_c');
            $grid->column('back_pel_zlw');
            $grid->column('back_pel_ycq');
            $grid->column('back_pel_zcq');
            $grid->column('back_knj_zlw');
            $grid->column('back_knj_xwfm');
            $grid->column('back_knj_xnf');
            $grid->column('back_fot_yzg');
            $grid->column('back_fot_bpz');
            $grid->column('back_fot_gzg');
            $grid->column('side_img');
            $grid->column('back_img');
            $grid->column('side_comment');
            $grid->column('back_comment');
            $grid->column('side_summary');
            $grid->column('back_summary');
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');

            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new FitnessForm(), function (Show $show) {
            $show->field('id');
            $show->field('title');
            $show->field('user_id');
            $show->field('height');
            $show->field('weight');
            $show->field('arm_cir_left');
            $show->field('arm_cir_right');
            $show->field('side_head_zlw');
            $show->field('side_head_qy');
            $show->field('side_head_hy');
            $show->field('side_cvb_zxz');
            $show->field('side_cvb_qq');
            $show->field('side_cvb_qz');
            $show->field('side_pat_zlw');
            $show->field('side_pat_nx');
            $show->field('side_thv_zxz');
            $show->field('side_thv_ht');
            $show->field('side_thv_qz');
            $show->field('side_thv_fgb');
            $show->field('side_luv_zxz');
            $show->field('side_luv_qq');
            $show->field('side_luv_qz');
            $show->field('side_ntp_zlw');
            $show->field('side_ntp_qq');
            $show->field('side_ntp_hq');
            $show->field('side_sar_zlw');
            $show->field('side_sar_cs');
            $show->field('back_nec_zlw');
            $show->field('back_nec_qq');
            $show->field('back_nec_xz');
            $show->field('back_sho_zlw');
            $show->field('back_sho_yg');
            $show->field('back_sho_zg');
            $show->field('back_pat_zlw');
            $show->field('back_pat_qq');
            $show->field('back_pat_hsk');
            $show->field('back_tlv_zlw');
            $show->field('back_tlv_s');
            $show->field('back_tlv_c');
            $show->field('back_pel_zlw');
            $show->field('back_pel_ycq');
            $show->field('back_pel_zcq');
            $show->field('back_knj_zlw');
            $show->field('back_knj_xwfm');
            $show->field('back_knj_xnf');
            $show->field('back_fot_yzg');
            $show->field('back_fot_bpz');
            $show->field('back_fot_gzg');
            $show->field('side_img');
            $show->field('back_img');
            $show->field('side_comment');
            $show->field('back_comment');
            $show->field('side_summary');
            $show->field('back_summary');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $userId = request()->has('user_id') ? request()->input('user_id') : 0;
        $user = User::find($userId);

        return Form::make(FitnessForm::with('user'), function (Form $form) use ($user) {
            $form->block(12, function (Form\BlockForm $form) use ($user) {
                $form->title('基本资料');
                $form->column(6, function (Form\BlockForm $form) use ($user) {
                    $form->display('id');
                    $form->text('title')->default('体态评估表');
                    $form->select('user_id', '用户')->options(User::get()->pluck('name', 'id')->toArray())->default($user->id);
                    $form->select('user.gender')->options([0 => '女', 1 => '男'])->default($user->gender);
                    $form->date('user.birthday');
                });

                $form->column(6, function (Form\BlockForm $form) {
                    $form->decimal('height')->help('cm');
                    $form->decimal('weight')->help('kg');
                    $form->text('arm_cir_left');
                    $form->text('arm_cir_right');
                });
            });

            $form->block(12, function (Form\BlockForm $form) {
                $form->title('侧面观');
                $form->column(12, function (Form\BlockForm $form) {
                    $form->checkbox('side_head_zlw')->options([1 => '中立位']);
                    $form->checkbox('side_head_qy')->options([1 => ' ']);
                });
            });

            $form->block(6, function (Form\BlockForm $form) {
                $form->title('背面观');
            });

            $form->text('side_head_qy');
            $form->text('side_head_hy');
            $form->text('side_cvb_zxz');
            $form->text('side_cvb_qq');
            $form->text('side_cvb_qz');
            $form->text('side_pat_zlw');
            $form->text('side_pat_nx');
            $form->text('side_thv_zxz');
            $form->text('side_thv_ht');
            $form->text('side_thv_qz');
            $form->text('side_thv_fgb');
            $form->text('side_luv_zxz');
            $form->text('side_luv_qq');
            $form->text('side_luv_qz');
            $form->text('side_ntp_zlw');
            $form->text('side_ntp_qq');
            $form->text('side_ntp_hq');
            $form->text('side_sar_zlw');
            $form->text('side_sar_cs');
            $form->text('back_nec_zlw');
            $form->text('back_nec_qq');
            $form->text('back_nec_xz');
            $form->text('back_sho_zlw');
            $form->text('back_sho_yg');
            $form->text('back_sho_zg');
            $form->text('back_pat_zlw');
            $form->text('back_pat_qq');
            $form->text('back_pat_hsk');
            $form->text('back_tlv_zlw');
            $form->text('back_tlv_s');
            $form->text('back_tlv_c');
            $form->text('back_pel_zlw');
            $form->text('back_pel_ycq');
            $form->text('back_pel_zcq');
            $form->text('back_knj_zlw');
            $form->text('back_knj_xwfm');
            $form->text('back_knj_xnf');
            $form->text('back_fot_yzg');
            $form->text('back_fot_bpz');
            $form->text('back_fot_gzg');
            $form->text('side_img');
            $form->text('back_img');
            $form->text('side_comment');
            $form->text('back_comment');
            $form->text('side_summary');
            $form->text('back_summary');

            $form->display('created_at');
            $form->display('updated_at');
        });
    }

    /**
     * @param Content $content
     * @param Request $request
     * @return Content
     * @throws \Throwable
     */
    public function userFitness(Content $content, Request $request)
    {
        $userId = $request->input('user_id');
        $user = User::find($userId);

        $form = FitnessForm::where('user_id', $userId)
            ->get()->first();

        $today = date('Y-m-d', time());
        $content->title('体态评估表');
        $content->description('');
        return $content->body(admin_view('fitness_form', compact('today', 'user', 'userId', 'form')));
    }

    /**
     * @param Content $content
     * @param Request $request
     * @return \Illuminate\Http\RedirectResponse
     */
    public function addUserFitness(Content $content, Request $request)
    {

        $userId = $request->input('user_id');
        $createDate = $request->input('created_date');
        $height = $request->input('height');
        $weight = $request->input('weight');
        $armCirLeft = $request->input('arm_cir_left');
        $armCirRight = $request->input('arm_cir_right');

        $formId = $request->input('form_id');

        if (empty($formId)) {
            $form = new FitnessForm();
        } else {
            $form = FitnessForm::find($formId);
        }

        DB::beginTransaction();

        try {

            $form->created_date = $createDate;
            $form->height = $height;
            $form->weight = $weight;
            $form->arm_cir_left = $armCirLeft;
            $form->arm_cir_right = $armCirRight;
            $form->user_id = $userId;
            $form->title = '体态评估表';

            // 侧面观
            // 1. 头部
            $form->side_head_zlw = $request->input('side_head_zlw');
            $form->side_head_qy = $request->input('side_head_qy');
            $form->side_head_hy = $request->input('side_head_hy');
            // 2. 颈椎
            $form->side_cvb_zxz = $request->input('side_cvb_zxz');
            $form->side_cvb_qq = $request->input('side_cvb_qq');
            $form->side_cvb_qz = $request->input('side_cvb_qz');
            // 3. 扁胛骨
            $form->side_pat_zlw = $request->input('side_pat_zlw');
            $form->side_pat_nx = $request->input('side_pat_nx');
            // 4. 胸椎
            $form->side_thv_zxz = $request->input('side_thv_zxz');
            $form->side_thv_ht = $request->input('side_thv_ht');
            $form->side_thv_qz = $request->input('side_thv_qz');
            $form->side_thv_qz = $request->input('side_thv_qz');
            $form->side_thv_fgb = $request->input('side_thv_fgb');
            // 5. 腰椎
            $form->side_luv_zxz = $request->input('side_luv_zxz');
            $form->side_luv_qq = $request->input('side_luv_qq');
            $form->side_luv_qz = $request->input('side_luv_qz');
            // 6. 骨盆
            $form->side_ntp_zlw = $request->input('side_ntp_zlw');
            $form->side_ntp_qq = $request->input('side_ntp_qq');
            $form->side_ntp_hq = $request->input('side_ntp_hq');
            // 7. 脓关节
            $form->side_sar_zlw = $request->input('side_sar_zlw');
            $form->side_sar_cs = $request->input('side_sar_cs');

            // 背面观
            // 1. 颈部
            $form->back_nec_zlw = $request->input('back_nec_zlw');
            $form->back_nec_qq = $request->input('back_nec_qq');
            $form->back_nec_xz = $request->input('back_nec_xz');
            // 2. 肩部
            $form->back_sho_zlw = $request->input('back_sho_zlw');
            $form->back_sho_yg = $request->input('back_sho_yg');
            $form->back_sho_zg = $request->input('back_sho_zg');
            // 3. 扁胛骨
            $form->back_pat_zlw = $request->input('back_pat_zlw');
            $form->back_pat_qq = $request->input('back_pat_qq');
            $form->back_pat_hsk = $request->input('back_pat_hsk');
            // 4. 胸腰椎
            $form->back_tlv_zlw = $request->input('back_tlv_zlw');
            $form->back_tlv_s = $request->input('back_tlv_s');
            $form->back_tlv_c = $request->input('back_tlv_c');
            // 5. 骨盆
            $form->back_pel_zlw = $request->input('back_pel_zlw');
            $form->back_pel_ycq = $request->input('back_pel_ycq');
            $form->back_pel_zcq = $request->input('back_pel_zcq');
            // 6. 膝关节
            $form->back_knj_zlw = $request->input('back_knj_zlw');
            $form->back_knj_xwfm = $request->input('back_knj_xwfm');
            $form->back_knj_xnf = $request->input('back_knj_xnf');
            // 7. 足部
            $form->back_fot_yzg = $request->input('back_fot_yzg');
            $form->back_fot_bpz = $request->input('back_fot_bpz');
            $form->back_fot_gzg = $request->input('back_fot_gzg');

            $form->side_comment = $request->input('side_comment');
            $form->side_summary = $request->input('side_summary');
            $form->back_comment = $request->input('back_comment');
            $form->back_summary = $request->input('back_summary');

            $form->save();

            DB::commit();

        } catch (\Exception $e) {
            DB::rollBack();
            dd($e);
        }

        $content->withSuccess('保存成功');
        return redirect()->back();


    }
}
